package com.guit.hrms.mapper;

import com.guit.hrms.dto.recuitmentDTO.DelPositions;
import com.guit.hrms.po.recruitmentPO.PlanDeptJob;
import com.guit.hrms.po.recruitmentPO.Recruitment;
import com.guit.hrms.po.recruitmentPO.FileEntity;
import com.guit.hrms.vo.recruitmentVO.PlanDeptJobVO;
import com.guit.hrms.vo.recruitmentVO.RecruitmentVO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface RecruitmentMapper {

    /**
     * 新增招聘公告，并返回主键id：planId
     * @param recruitment 招聘公告信息
     * @return 新增的招聘公告的id
     */
    @Options(keyProperty = "planId",useGeneratedKeys = true)
    @Insert("insert into " +
            "recruitment_plan (plan_name, plan_code, plan_type, recruiter_id, recruiter_name, " +
            "recruiter_phone, recruitment_conditions, benefits_description, start_date, end_date, publish_date, " +
            "interview_process, approve_status, approver_id, " +
            "notes, created_by, updated_by, created_at, updated_at,content)" +
            "value (#{planName},#{planCode},#{planType},#{recruiterId},#{recruiterName}," +
            "#{recruiterPhone},#{recruitmentConditions},#{benefitsDescription},#{startDate},#{endDate},#{publishDate}," +
            "#{interviewProcess},#{approveStatus},#{approverId}," +
            "#{notes},#{createdBy},#{updatedBy},#{createdAt},#{updatedAt},#{content})")
    Integer newRecruitment(Recruitment recruitment);

    /**
     * 新增招聘计划表的同时一同添加招聘计划部门岗位表
     * @param planDeptJob 招聘计划部门岗位信息
     */
    @Insert("insert into plan_dept_job(plan_id, dept_id, position_number, position_name, position_properties," +
            "job_description, job_required, person,professional_skills_right,communication_skills_right,teamwork_ability_right) " +
            "value (#{planId},#{numbering},#{positionNumber},#{positionName},#{positionProperties}," +
            "#{jobDescription},#{jobRequired},#{person},#{professionalSkillsRight},#{communicationSkillsRight},#{teamworkAbilityRight})")
    void newPlanDeptJob(PlanDeptJob planDeptJob);

    /**
     * 新增招聘计划表的同时一同添加招聘计划二级学院表
     * @param planDeptJob 招聘计划部门岗位信息
     */
    @Insert("insert into plan_academy_job(plan_id, academy_id, position_number, position_name, " +
            "job_description, job_required, person,professional_skills_right,communication_skills_right,teamwork_ability_right) " +
            "value (#{planId},#{numbering},#{positionNumber},#{positionName}," +
            "#{jobDescription},#{jobRequired},#{person},#{professionalSkillsRight},#{communicationSkillsRight},#{teamworkAbilityRight})")
    void newPlanAcademyJob(PlanDeptJob planDeptJob);

    /**
     * 添加招聘公告附件
     * @param planId 招聘计划id
     * @param file 附件
     * @param fileName 文件名
     * @param contentType 内容类型
     * @return 影响的行数
     */
    @Update("UPDATE recruitment_plan SET " +
            "attachments = #{file}, " +
            "file_name = #{fileName}, " +
            "content_type = #{contentType} " +
            "WHERE plan_id = #{planId}")
    Integer addFileToRecruitmentPlan(
        Integer planId, 
        byte[] file,
        String fileName,
        String contentType
    );

    /**
     * 查询招聘计划表
     * @return 招聘计划表
     */
    List<RecruitmentVO> queryRecruitmentPlan();


    /**
     * 根据planId获取deptId集合*(查询plan_dept_job)
     * @param planId 招聘计划id
     * @return deptId集合
     */
    List<String> getDeptIdByPlanId(Integer planId);

    /**
     * 根据planId，deptId获取positionName集合(查询plan_dept_job)
     * @param planId 招聘计划id
     * @param deptId 部门id
     * @return positionName集合
     */
    List<String> getPositionNameByPlanIdAndDeptId(Integer planId, String deptId);

    /**
     * 根据所有主键获取列表信息(查询plan_dept_job)
     * @param planId 招聘计划id
     * @param deptId 部门id
     * @param positionName 岗位名称
     * @return 列表信息
     */
    @Select("select *from plan_dept_job  where plan_id = #{planId} and dept_id = #{deptId} and position_name = #{positionName}")
    PlanDeptJobVO getByAllPrimaryKey(Integer planId, String deptId, String positionName);

    /**
    * 根据planId获取academyId集合(查询plan_academy_job)
    * @param planId 招聘计划id
    * @return academyId集合
    */
    @Select("select distinct academy_id from plan_academy_job where plan_id = #{planId}")
    List<String> getAcademyIdByPlanId(Integer planId);
    
    /**
     * 根据planId，academyId获取positionName集合(查询plan_academy_job)
     * @param planId 招聘计划id
     * @param academyId 学院id
     * @return positionName集合
     */
    @Select("select distinct position_name from plan_academy_job where plan_id = #{planId} and academy_id = #{academyId}")
    List<String> getPositionNameByPlanIdAndAcademyId(Integer planId, String academyId);
    
    /**
     * 根据所有主键获取列表信息(查询plan_academy_job)
     * @param planId 招聘计划id
     * @param academyId 学院id
     * @param positionName 岗位名称
     * @return 列表信息
     */
    @Select("select * from plan_academy_job where plan_id = #{planId} and academy_id = #{academyId} and position_name = #{positionName}")
    PlanDeptJobVO getByAllPrimaryKeyFromAcademy(Integer planId, String academyId, String positionName);

    /**
     * 修改招聘公告（管理岗位）
     * @param planDeptJob 修改信息
     */
    Integer updatePlanDeptJob(PlanDeptJob planDeptJob);

    /**
     * 修改招聘公告（二级学院）
     * @param planDeptJob 修改信息
     */
    Integer updatePlanAcademyJob(PlanDeptJob planDeptJob);

    /**
     * 修改其他招聘公告
     * @param planDeptJob 修改信息
     * @param updateAt 修改时间
     * @param updateBy 修改人
     * @return 影响的行数
     */
    @Update("update recruitment_plan set content = #{planDeptJob.content}, updated_by = #{updateBy}, updated_at = #{updateAt} where plan_id = #{planDeptJob.planId}")
    Integer updateOtherPlan(PlanDeptJob planDeptJob, LocalDateTime updateAt, String updateBy);

    /**
     * 通过planId删除部门岗位表
     * @param planId 招聘计划id
     */
    @Delete("delete from plan_dept_job where plan_id = #{planId}")
    Integer deletePlanDeptJobById(Integer planId);

    /**
     * 根据planId删除二级学院岗位表
     * @param planId 招聘计划id
     * @return 影响的行数
     */
    @Delete("delete from plan_academy_job where plan_id = #{planId}")
    Integer deletePlanAcademyJobById(Integer planId);

    /**
     * 通过planId删除计划表
     * @param planId 招聘计划id
     */
    @Delete("delete from recruitment_plan where plan_id = #{planId}")
    Integer deleteRecruitmentPlanByPlanId(Integer planId);

    /**
     * 通过planId查询计划表
     * @param planId 招聘计划id
     * @return 招聘计划表
     */
    @Select("select * from recruitment_plan where plan_id = #{planId}")
    Recruitment getRecruitmentPlanByPlanId(Integer planId);

    /**
     * 根据所有主键删除部门岗位表
     * @param planDeptJob 部门岗位信息
     */
    @Delete("delete from plan_dept_job where plan_id = #{planId} and dept_id = #{numbering} and position_name = #{positionName}")
    void deletePlanDeptJobByAllPrimaryKey(DelPositions planDeptJob);

    /**
     * 根据所有主键删除二级学院岗位表
     * @param planDeptJob 部门岗位信息
     */
    @Delete("delete from plan_academy_job where plan_id = #{planId} and  academy_id= #{numbering} and position_name = #{positionName}")
    void deletePlanAcademyJobByAllPrimaryKey(DelPositions planDeptJob);

    /**
     * 根据岗位名字查询部门
     * @param positionName 岗位名字
     * @return 结果
     */
    @Select("select * from  plan_dept_job where position_name = #{positionName}")
    List<PlanDeptJob> getDepartmentByPositionName(String positionName);


    /**
     * 先根据planId查询type
     * @param planId 招聘计划id
     * @return 结果
     */
    @Select("select plan_type from recruitment_plan where plan_id = #{planId}")
    Integer getPlanTypeByPlanId(Integer planId);

    /**
     * 查询plan_dept_job
     * @param planId 招聘计划id
     * @param deptId 部门id
     * @param positionNumber 岗位编号
     * @return 结果
     */
    @Select("select * from plan_dept_job  where plan_id = #{planId} and dept_id = #{deptId} and position_number = #{positionNumber}")
    PlanDeptJobVO getPlanDeptJob(Integer planId, String deptId, String positionNumber);

    /**
     * 查询plan_academy_job
     * @param planId 招聘计划id
     * @param deptId 部门id
     * @param positionNumber 岗位编号
     * @return 结果
     */
    @Select("select * from plan_academy_job  where plan_id = #{planId} and academy_id = #{deptId} and position_number = #{positionNumber}")
    PlanDeptJobVO getPlanAcademyJob(Integer planId, String deptId, String positionNumber);

    /**
     * 更新招聘计划表
     * @param planId 招聘计划id
     * @param updatedAt 更新时间
     * @param updatedBy 更新人
     * @return 结果
     */
    @Update("update recruitment_plan set approve_status = 1, updated_by = #{updatedBy}, updated_at = #{updatedAt} where plan_id = #{planId}")
    Integer updateRecruitmentPlan(Integer planId, LocalDateTime updatedAt, String updatedBy);

    /**
     * 更新招聘公告附件信息
     */
    @Update("UPDATE recruitment_plan SET " +
            "attachments = #{attachments}, " +
            "file_name = #{fileName}, " +
            "content_type = #{contentType}, " +
            "updated_at = #{updatedAt} " +
            "WHERE plan_id = #{planId}")
    Integer updateAttachment(FileEntity fileEntity);
}
